home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / diskutil / unixflop.lzh / TAR2RAW.C < prev    next >
C/C++ Source or Header  |  1987-04-22  |  2KB  |  101 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <osbind.h>
  4.  
  5. int size = 0;
  6. int fd, s = 0, s_max = 512 * 9 * 2 * 80;
  7. int track = 0, side = 0;
  8. int block=0;
  9. char buf[5120];
  10. char filename[80];
  11.  
  12. int
  13. main(argc,argv)
  14. int argc;
  15. char *argv[];
  16. {
  17.     puts("Insert raw disk in drive A. Hit CR to read file D:\\A.TAR\n");
  18.     puts("or type file name: ");
  19.     gets(filename);
  20.     if (!filename[0])
  21.         strcpy(filename, "D:\\A.TAR");
  22.     fd = open(filename, O_RDONLY | O_BINARY);
  23.     if (!fd) {
  24.         char mess[80];
  25.         sprintf(mess, "Cannot open file %d\n", filename);
  26.         fatal_error(mess);
  27.     }
  28.     for (;;) {
  29.         if(convert_track(1, 0)) break;
  30.     if(convert_track(0, 1)) break;
  31.     }
  32.     close(fd);
  33.     puts("Ok, done. Hit CR.\n");
  34.     getchar();
  35.     return 0;
  36. }
  37.  
  38. convert_track(next_side, incr_track)
  39. int next_side, incr_track;
  40. {
  41.     static long total_size;
  42.     int read_size = read(fd, buf, 512 * 9);
  43.     total_size += read_size;
  44.     if (read_size != 512 * 9) {
  45.         s_max=total_size/512/9;
  46.         bzero(buf+read_size, 512*9 - read_size);
  47.     }
  48.     if (Flopwr(buf, 0L, 0, 1, track, side, 9)) {
  49.         char mess[80];
  50.         sprintf(mess, "error writing track %d, side %d\n", track, side);
  51.         fatal_error(mess);
  52.     }
  53.     block = print_names(block, s, s +9 < s_max ? s+9 : s_max);
  54.     side = next_side; s += 9; track += incr_track;
  55.     if (read_size != 512 * 9) {
  56.         long pos = lseek(fd, 0L, 2);
  57.         if (pos == -1) {
  58.             fatal_error("could not reach end of file\n");
  59.         } else if (pos != total_size) {
  60.             fatal_error("cannot read whole file\n");
  61.         } else if (read_size > 512 * 7) {
  62.             return 0;
  63.         } else {
  64.             return 1;
  65.         }
  66.     } else
  67.         return 0;
  68. }
  69.  
  70. int print_names(block, s, s_end)
  71. int block, s, s_end;
  72. {
  73.     /* printf("Block %d, s = %d, s_end = %d\n", block,s,s_end); */
  74.     while (block >= s && block < s_end) {
  75.         if (buf[(block-s) * 512] == '\0') {
  76.         /* end of tape reached */
  77.             printf("End of file at block %d\n", block);
  78.             block = -1;
  79.             s_max = block + 2;
  80.         } else {
  81.             long size;
  82.             sscanf(buf + (block-s) * 512 + 124, " %lo", &size);
  83.             printf("Block %d: %.100s %ld\n", block, buf + (block-s) * 512, size);
  84.             if (size)
  85.                 block = block + 2 + (int) ((size - 1)/ 512);
  86.             else
  87.                 block++;
  88.          }
  89.     }
  90.     return block;
  91. }
  92.  
  93. fatal_error(s)
  94. char *s;
  95. {
  96.     puts(s);
  97.     puts("\007Aborting... Press CR\n");
  98.     getchar();
  99.     exit(1);
  100. }
  101.